﻿//------------------------------------------------------------------------------
// <copyright file="Hotkey Change Window.xaml.cs" company="Robert MacLean">
//      Copyright © Robert MacLean.  All Rights Reserved.
// </copyright>
//
//  Details on this see: http://rule18.codeplex.com
//  This code released under the terms of the 
//  Microsoft Reciprocal License (MS-RL, http://opensource.org/licenses/ms-rl.html.)
//------------------------------------------------------------------------------

namespace Rule18
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    using System.Globalization;
    using Rule18.Core;
    using System.Diagnostics.Contracts;

    /// <summary>
    /// Interaction logic for Hotkey_Change_Window.xaml
    /// </summary>
    public partial class HotkeyChangeWindow : Window
    {
        /// <summary>
        /// Gets the key combination which was last pressed.
        /// </summary>
        /// <value>The key.</value>
        public KeyInfo Key {get; private set;}

        /// <summary>
        /// Initializes a new instance of the <see cref="HotkeyChangeWindow"/> class.
        /// </summary>
        public HotkeyChangeWindow()
        {
            Contract.Ensures(this.Key != null);

            InitializeComponent();
            this.Key = new KeyInfo();
        }

        /// <summary>
        /// Handles the KeyDown event of the Window control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Input.KeyEventArgs"/> instance containing the event data.</param>
        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            Contract.Requires(e != null);

            System.Windows.Forms.Keys key;
            if (e.Key == System.Windows.Input.Key.System)
            {
                Enum.TryParse(e.SystemKey.ToString(), out key);
            }
            else
            {
                Enum.TryParse(e.Key.ToString(), out key);
            }

            this.Key.Key = key;

            this.Key.Alt = Keyboard.Modifiers.HasFlag(ModifierKeys.Alt);
            this.Key.Shift = Keyboard.Modifiers.HasFlag(ModifierKeys.Shift);
            this.Key.Ctrl = Keyboard.Modifiers.HasFlag(ModifierKeys.Control);

            PressingTextBlock.Text = string.Format(CultureInfo.CurrentCulture, "You pressed: {0}", this.Key);

            OKButton.IsEnabled = (this.Key.Ctrl || this.Key.Alt) && (this.Key.Key != System.Windows.Forms.Keys.None);
        }

        /// <summary>
        /// Handles the Click event of the OKButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
            this.Close();
        }       
    }
}
